/**
 * Copyright (C) 2011-2012 GreenSoft S.A.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.greensoft.neosystem.user;

import java.util.List;

import javax.inject.Inject;

import ar.com.greensoft.neosystem.dao.LocalidadDao;
import ar.com.greensoft.neosystem.dao.ProvinciaDao;
import ar.com.greensoft.neosystem.model.Persona;
import ar.com.greensoft.neosystem.model.address.Localidad;
import ar.com.greensoft.neosystem.model.address.Pais;
import ar.com.greensoft.neosystem.model.address.Provincia;

/**
 * 
 * @author Jorge E. Villaverde
 * @since 1.0.0
 * @version 1.0.0
 */
public abstract class PersonaBean<P extends Persona> 
	extends AbstractLongIdUserRoleCrudFacedeBean<P> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1576441559695950863L;

    @Inject
    private LocalidadDao localidadDao;
    @Inject
    private ProvinciaDao provinciaDao;
    
    protected Pais pais;
    
    protected Provincia provincia;

	public Pais getPais() {
		return pais;
	}

	public void setPais(Pais pais) {
		this.pais = pais;
	}

	public Provincia getProvincia() {
		return provincia;
	}

	public void setProvincia(Provincia provincia) {
		this.provincia = provincia;
	}
	
	public List<Provincia> getProvincias(){
		if(this.pais == null)
			return null;
		return provinciaDao.findByPais(pais);
	}
	
	public List<Localidad> getLocalidades(){
		if(this.provincia == null)
			return null;
		return localidadDao.findByProvincia(provincia);
	}
	
	@Override
	public String showUpdate() {
		String returnPage = super.showUpdate();
		Persona entity = getEntity(); 
		if(entity == null)
			return showSearch();
		if(entity.getDireccion().getLocalidad() != null){
			this.provincia = entity.getDireccion().getLocalidad().getProvincia();
			this.pais = entity.getDireccion().getLocalidad().getProvincia().getPais();
		}		
		return returnPage;
	}	
}
